package com.taimanetworks.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;

public class ClickUtils {

    private static Connection connection;

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        //Class.froName装载一个类并且对其进行实例化
        Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
        Random rd = new Random();
         int num = rd.nextInt();
        int b = num % 3;
        String url = "jdbc:clickhouse://172.22.2.80:8123/default?socket_timeout=300000";
/*        switch (b){
             case 0:url = "jdbc:clickhouse://172.22.2.80:8123/default?socket_timeout=300000"; break;
             case 1:url = "jdbc:clickhouse://172.22.2.81:8123/default?socket_timeout=300000"; break;
             case 2:url = "jdbc:clickhouse://172.22.2.82:8123/default?socket_timeout=300000"; break;
//             default:url = "jdbc:clickhouse://172.22.2.80:8123/default?socket_timeout=300000";
         }*/
//        String url = "jdbc:clickhouse://172.22.2.80:8123/default";
        connection = DriverManager.getConnection(url);
        return connection;
    }

    public void close() throws SQLException {
        connection.close();
    }

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Connection connection =  getConnection();


        String sql="INSERT into car_test2 ( vin ,gpss_time ,gpss_type) VALUES (?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,"1");
        preparedStatement.setString(2,"2022-07-27 00:00:00");
        preparedStatement.setString(3,"a");

        preparedStatement.addBatch();

        long startTime = System.currentTimeMillis();
        int[] ints = preparedStatement.executeBatch();
//        connection.commit();
        long endTime = System.currentTimeMillis();
        System.out.println("批量插入完毕用时：" + (endTime - startTime) + " -- 插入数据 = " + ints.length);


    }
}
